package com.friend.base.controller.base;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.friend.base.service.base.MemberCircleService;
import com.friend.base.service.base.MemberService;
import com.friend.base.service.base.MemberSpaceService;
import com.friend.base.service.base.SpaceService;
import com.friend.common.model.BaseController;
import com.friend.common.model.Result;
import com.friend.entity.entity.base.MemberCircle;
import com.friend.entity.entity.base.MemberSpace;
import com.friend.entity.entity.base.Space;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author friend
 * @since 2019-10-17
 */
@Api(tags = {"活动场前台控制器"})
@Slf4j
@RestController
@RequestMapping(value = "/space", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class SpaceController extends BaseController {

    @Autowired
    private SpaceService spaceService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberSpaceService memberSpaceService;
    @Autowired
    private MemberCircleService memberCircleService;

    @GetMapping("/getCirSpace")
    @ApiOperation(value = "获取活动场", notes = "根据圈子主键获取活动场", httpMethod = "GET")
    public Result getCirSpace(@RequestParam("circleId") String circleId, @RequestParam("memId")  Integer memId){
        try {
            List<MemberSpace> memberSpaces = memberSpaceService.list(new QueryWrapper<MemberSpace>().select("spaceId").eq("memId", memId));
            List<Integer> spaceIds = memberSpaces.stream().map(relate -> relate.getSpaceId()).collect(Collectors.toList());
            int circleCount = memberCircleService.count(new QueryWrapper<MemberCircle>().eq("circleId", circleId));
            List<Space> spaces = spaceService.list(new QueryWrapper<Space>().eq("circleId", circleId).orderByDesc("create_time"));
            spaces.forEach(space -> {
                space.setCreateUserStr(memberService.getById(space.getCreateUser()).getNickname());
                int spaceCount = memberSpaceService.count(new QueryWrapper<MemberSpace>().eq("spaceId", space.getId()));
                space.setSpaceCount(spaceCount);
                space.setCircleCount(circleCount);
                if (CollectionUtil.isNotEmpty(spaceIds) && spaceIds.contains(space.getId())){
                    space.setJoinFlag(1);
                }else {
                    space.setJoinFlag(0);
                }
            });
            return createSuccessResult(spaces);
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("场子列表查询失败");
        }
    }

    @GetMapping("/getSpaceInfo")
    @ApiOperation(value = "获取活动场", notes = "根据圈子主键获取活动场", httpMethod = "GET")
    public Result getSpaceInfo(@RequestParam("spaceId") String spaceId, @RequestParam("memId")  Integer memId){
        try {
            Space space = spaceService.getById(spaceId);
            if (!spaceId.equals(memId)){
                int count = memberSpaceService.count(new QueryWrapper<MemberSpace>().eq("spaceId", spaceId).eq("memId", memId));
                if (count > 0){
                    space.setJoinFlag(1);
                }else  {
                    space.setJoinFlag(0);
                }
            }
            return createSuccessResult(space);
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("活动信息查询失败");
        }
    }

    @PostMapping("/saveSpace")
    @ApiOperation(value = "保存场子", notes = "保存会员创建的场子", httpMethod = "POST")
    public Result saveSpace(@RequestBody Space space){
        try {
            space.setUpdateUser(space.getCreateUser());
            boolean flag = spaceService.save(space);
            if (flag){
                log.info("保存场子,保存时间date-{},场子信息-{}",LocalDate.now(),space);
                return this.createSuccessResult(flag);
            }
            return this.createFailedResult("保存场子失败");
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("保存场子失败");
        }
    }
}